﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using Intorise.Framework;

namespace Intorise.Common
{
    public static class Tools
    {
        private static Security security = new Security();
        /// <summary>
        /// 将不可显示字符替换为"."
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ConvertChar(string s)
        {
            char[] chars = s.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                if (Char.IsControl(chars[i]) || char.IsWhiteSpace(chars[i]))
                {
                    chars[i] = '.';
                }
            }
            return new string(chars);
        }

        /// <summary>
        /// 将给定的代码字节数组连接为一个数组
        /// </summary>
        /// <param name="data1"></param>
        /// <param name="data2"></param>
        /// <param name="data3"></param>
        /// <returns></returns>
        public static byte[] Contact(byte[] data1, byte[] data2, params byte[][] data3)
        {
            int length = 0;
            length = data1.Length + data2.Length;
            if (data3.Length > 0)
            {
                foreach (byte[] d in data3)
                {
                    length += d.Length;
                }
            }

            byte[] data = new byte[length];
            Array.Copy(data1, data, data1.Length);
            Array.Copy(data2, 0, data, data1.Length, data2.Length);

            int index = data1.Length + data2.Length;
            foreach (byte[] d in data3)
            {
                Array.Copy(d, 0, data, index, d.Length);
                index += d.Length;
            }
            return data;
        }

        /// <summary>
        /// 将给定的字节数组和字节连接为一个数组
        /// </summary>
        /// <param name="data1"></param>
        /// <param name="data2"></param>
        /// <returns></returns>
        public static byte[] Contact(byte[] data1, params byte[] data2)
        {
            int length = 0;
            length = data1.Length + data2.Length;

            byte[] data = new byte[length];
            Array.Copy(data1, data, data1.Length);
            Array.Copy(data2, 0, data, data1.Length, data2.Length);
            return data;
        }

        /// <summary>
        /// 从指定字节数组中查找数据，返回是否有相同数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <param name="searchedData"></param>
        /// <returns></returns>
        public static bool Search(byte[] data, int length, byte[] searchedData)
        {
            bool searched = true;
            for (int i = 0; i < length - searchedData.Length; i++)
            {
                searched = true;
                for (int j = 0; j < searchedData.Length; j++)
                {
                    if (searchedData[j] != data[i + j])
                    {
                        searched = false;
                        break;
                    }
                }
                if (searched)
                {
                    return true;
                }
            }
            return searched;
        }

        /// <summary>
        /// 比较字节数组是否相同
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <param name="targeData"></param>
        /// <returns></returns>
        public static bool Compare(byte[] data, byte[] targetData)
        {
            if (targetData.Length != data.Length)
            {
                return false;
            }

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] != targetData[i])
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 从指定的字节数组中查找字符串，返回是否存在
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <param name="searchedData"></param>
        /// <returns></returns>
        public static bool Search(byte[] data, int length, string searchedData)
        {
            string s = UTF8Encoding.UTF8.GetString(data, 0, length);
            return s.IndexOf(searchedData) > -1;
        }

        public static string GetHexDataString(string text)
        {
            byte[] datas = Encoding.UTF8.GetBytes(text);
            text = "";
            foreach (byte data in datas)
            {
                text += (data.ToString("X").PadLeft(2, '0') + " ");
            }
            return text;
        }

        public static string GetString(byte[] data)
        {
            return Encoding.UTF8.GetString(data);
        }
        public static string GetString(byte[] data, int length)
        {
            return Encoding.UTF8.GetString(data, 0, length);
        }
        public static string GetString(byte[] data, int index, int length)
        {
            return Encoding.UTF8.GetString(data, index, length);
        }
        public static byte[] GetByteFromHexString(string text)
        {
            string[] temp = text.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (temp.Length == 0)
            {
                throw new ArgumentException("文本不是用逗号或空格分隔的十六进制数字文本");
            }
            List<byte> list = new List<byte>();
            byte data = 0;
            foreach (string s in temp)
            {
                if (byte.TryParse(s, System.Globalization.NumberStyles.HexNumber, null, out data))
                {
                    list.Add(data);
                }
                else
                {
                    throw new ArgumentException("文本中包含的文本不是十六进制数字：" + s);
                }
            }

            return list.ToArray();
        }

        public static byte[] GetAscII(string text)
        {
            return Encoding.ASCII.GetBytes(text);
        }

        public static string GetStringFromHexString(string text)
        {
            byte[] datas = GetByteFromHexString(text);
            return Encoding.UTF8.GetString(datas);
        }

        public static string SubString(string text, string beginTag, string endTag, bool excepetTag = false)
        {
            int start = text.IndexOf(beginTag);
            if (start < 0) { return ""; }
            int end = text.IndexOf(endTag, start + beginTag.Length);
            if (end < 0) { return ""; }
            if (excepetTag)
            {
                return text.Substring(start + beginTag.Length, end - start - beginTag.Length);
            }
            else
            {
                return text.Substring(start, end - start + endTag.Length);
            }
        }

        public static string SubString(string text, string beginTag, bool excepetTag = false)
        {
            int start = text.IndexOf(beginTag);
            if (start < 0) { return ""; }
            if (excepetTag)
            {
                return text.Substring(start + beginTag.Length, text.Length - start - beginTag.Length);
            }
            else
            {
                return text.Substring(start, text.Length - start);
            }
        }

        /// <summary>
        /// 获取字节数组的十六进制字符串
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetHexString(byte[] datas, int length)
        {
            string s = "";
            for (int i = 0; i < length; i++)
            {
                s += datas[i].ToString("X2") + " ";
            }
            return s;
        }

        /// <summary>
        /// 获取第一个汉字的首字母，只能输入汉字
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string GetPYChar(string c)
        {
            byte[] array = new byte[2];
            array = System.Text.Encoding.Default.GetBytes(c);
            int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
            if (i < 0xB0A1) return "*";
            if (i < 0xB0C5) return "A";
            if (i < 0xB2C1) return "B";
            if (i < 0xB4EE) return "C";
            if (i < 0xB6EA) return "D";
            if (i < 0xB7A2) return "E";
            if (i < 0xB8C1) return "F";
            if (i < 0xB9FE) return "G";
            if (i < 0xBBF7) return "H";
            if (i < 0xBFA6) return "J";
            if (i < 0xC0AC) return "K";
            if (i < 0xC2E8) return "L";
            if (i < 0xC4C3) return "M";
            if (i < 0xC5B6) return "N";
            if (i < 0xC5BE) return "O";
            if (i < 0xC6DA) return "P";
            if (i < 0xC8BB) return "Q";
            if (i < 0xC8F6) return "R";
            if (i < 0xCBFA) return "S";
            if (i < 0xCDDA) return "T";
            if (i < 0xCEF4) return "W";
            if (i < 0xD1B9) return "X";
            if (i < 0xD4D1) return "Y";
            if (i < 0xD7FA) return "Z";
            return "*";
        }
    }
}
